﻿using System;
using System.Windows.Forms;
using UniMana.Utils.Data;
using System.Data;
using DevExpress.XtraEditors;
using System.Collections.Generic;
using UniMana.Data;
using UniMana.Utils.Configs;
using UniMana.Utils.LoginInfo;
using System.Drawing;
using DevExpress.XtraGrid.Views.Grid;

namespace UniMana.Win.Leadership
{
    public partial class Frm_Censorship : DevExpress.XtraEditors.XtraForm
    {
        #region[Vị trí cần thay đổi khi thêm yêu cầu duyệt mới]
        // - Frm_Censorship_Load
        // - GetListRequest()
        // - InitalGridView()
        // - gctView_Click
        // - ShowInfomation...()        
        #endregion

        #region [Biến]
        private DBMain m_data = new DBMain();
        private List<int> l_Department;
        // ChuyenKhoaNganh, BuocThoiHoc, SuaDiem ...
        public string type = "";
        public int typeRule;
        private DataTable m_tblTable;
        private DataTable m_tblCensorship;
        private bool _flagEdit = true;
        private bool m_bRequireStatus = false;

        string gcllogKey = "";
        string gcldepartmentKey = "";
        string gclrequireUser = "";
        int m_iLogKey = 0;
        int m_iDepartmentKey = 0;

        // 0 : Chua duyet, 1 : Agree, 2 : Refuse
        private int value1 = 0;
        private int value2 = 0;
        private int value3 = 0;
        private int value4 = 0;
        private int value5 = 0;
        private int value6 = 0;

        Training_StudentInfo studentInfo;
        Log_TranferInfo tranferInfo;
        Log_ForceCanceledInfo forceCancelInfo;
        Log_ScoreEditInfo scoreInfo;
        Training_TrainingProgramInfo programeInfo;
        Training_Student_ClassInfo stdClassInfo;

        public DevExpress.Utils.WaitDialogForm dlg;
        #endregion

        public Frm_Censorship(string type)
        {
            InitializeComponent();
            this.type = type;
        }

        private void Frm_Censorship_Load(object sender, EventArgs e)
        {
            LoadData();
            ShowDisplay(false);
            InitalGridView();
            GetListRequest();

            if (type == "ChuyenKhoaNganh")
                typeRule = 38;
            if (type == "BuocThoiHoc")
                typeRule = 40;
            if (type == "SuaDiem")
                typeRule = 36;
            //// Thêm yêu cầu mới
        }

        private void ShowDisplay(bool value)
        {
            if (value)
            {
                lctMain1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lctMain1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            }
            else
                lctMain1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
        }

        private void LoadData()
        {
            xucBar1.SaveVisible = true;
            xucBar1.CancelVisible = true;
            xucBar1.PrintVisible = true;
            xucBar1.AddNewVisible = false;
            xucBar1.DeleteVisible = false;
            xucBar1.EditVisible = false;
            xucBar1.ReLoadVisible = false;
            xucBar1.ImportExcelVisible = false;
            xucBar1.ExportExcelVisible = false;

            ShowCensorshipAll(false);
            l_Department = new List<int>();
            rluRequireUser.GetData(m_data.m_DCReadOnly.Personnel_Personnels.GetPersonnalAsRequireUser(), "RequireUser", "FullName");
            rluCatalogue_Department.GetData(m_data.m_DCReadOnly.Catalogue_Departments, "Catalogue_DepartmentKey", "Name");

            gluTraining_TrainingProgram1.GetData(m_data.m_DCReadOnly.Training_TrainingPrograms, "Training_TrainingProgramKey", "Name");
            gluTraining_TrainingProgram2.GetData(m_data.m_DCReadOnly.Training_TrainingPrograms, "Training_TrainingProgramKey", "Name");
            gluCatalogue_Department1.GetData(m_data.m_DCReadOnly.Catalogue_Departments, "Catalogue_DepartmentKey", "Name");
            gluCatalogue_Department2.GetData(m_data.m_DCReadOnly.Catalogue_Departments, "Catalogue_DepartmentKey", "Name");
            gluCatalogue_Major1.GetData(m_data.m_DCReadOnly.Catalogue_Majors, "Catalogue_MajorKey", "Name");
            gluCatalogue_Major2.GetData(m_data.m_DCReadOnly.Catalogue_Majors, "Catalogue_MajorKey", "Name");
            gluCatalogue_Level1.GetData(m_data.m_DCReadOnly.Catalogue_Levels, "Catalogue_LevelKey", "Name");
            gluCatalogue_Level2.GetData(m_data.m_DCReadOnly.Catalogue_Levels, "Catalogue_LevelKey", "Name");
            gluCatalogue_TrainingType1.GetData(m_data.m_DCReadOnly.Catalogue_TrainingTypes, "Catalogue_TrainingTypeKey", "Name");
            gluCatalogue_TrainingType2.GetData(m_data.m_DCReadOnly.Catalogue_TrainingTypes, "Catalogue_TrainingTypeKey", "Name");
            gluCatalogue_Course1.GetData(m_data.m_DCReadOnly.Catalogue_Courses, "Catalogue_CourseKey", "Name");
            gluCatalogue_Course2.GetData(m_data.m_DCReadOnly.Catalogue_Courses, "Catalogue_CourseKey", "Name");
            gluTraining_Class1.GetData(m_data.m_DCReadOnly.Training_Classes, "Training_ClassKey", "ID");
            gluTraining_Class2.GetData(m_data.m_DCReadOnly.Training_Classes, "Training_ClassKey", "ID");

            rluCatalogue_Level1.GetData(m_data.m_DCReadOnly.Catalogue_Levels, "Catalogue_LevelKey", "Name");
            rluCatalogue_Level2.GetData(m_data.m_DCReadOnly.Catalogue_Levels, "Catalogue_LevelKey", "Name");
        }

        #region [ShowCensorship]
        private void ShowCensorshipAll(bool value)
        {
            if (value)
            {
                ShowCensorship1(true);
                ShowCensorship2(true);
                ShowCensorship3(true);
                ShowCensorship4(true);
                ShowCensorship5(true);
                ShowCensorship6(true);
            }
            else
            {
                ShowCensorship1(false);
                ShowCensorship2(false);
                ShowCensorship3(false);
                ShowCensorship4(false);
                ShowCensorship5(false);
                ShowCensorship6(false);
            }
        }

        private void ShowCensorship1(bool value)
        {
            if (value)
            {
                lciDate1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lciAgree1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lciRefuse1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lciPerson1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lciDate1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                lciAgree1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                lciRefuse1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                lciPerson1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            }
            else
            {
                lciDate1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                lciAgree1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                lciRefuse1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                lciPerson1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            }
        }

        private void ShowCensorship2(bool value)
        {
            if (value)
            {
                lciDate2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lciAgree2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lciRefuse2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lciPerson2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lciDate2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                lciAgree2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                lciRefuse2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                lciPerson2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            }
            else
            {
                lciDate2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                lciAgree2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                lciRefuse2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                lciPerson2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            }
        }

        private void ShowCensorship3(bool value)
        {
            if (value)
            {
                lciDate3.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lciAgree3.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lciRefuse3.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lciPerson3.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lciDate3.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                lciAgree3.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                lciRefuse3.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                lciPerson3.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            }
            else
            {
                lciDate3.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                lciAgree3.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                lciRefuse3.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                lciPerson3.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            }
        }

        private void ShowCensorship4(bool value)
        {
            if (value)
            {
                lciDate4.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lciAgree4.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lciRefuse4.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lciPerson4.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lciDate4.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                lciAgree4.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                lciRefuse4.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                lciPerson4.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            }
            else
            {
                lciDate4.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                lciAgree4.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                lciRefuse4.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                lciPerson4.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            }
        }

        private void ShowCensorship5(bool value)
        {
            if (value)
            {
                lciDate5.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lciAgree5.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lciRefuse5.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lciPerson5.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lciDate5.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                lciAgree5.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                lciRefuse5.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                lciPerson5.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;

            }
            else
            {
                lciDate5.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                lciAgree5.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                lciRefuse5.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                lciPerson5.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            }
        }

        private void ShowCensorship6(bool value)
        {
            if (value)
            {
                lciDate6.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lciAgree6.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lciRefuse6.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lciPerson6.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.OnlyInRuntime;
                lciDate6.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                lciAgree6.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                lciRefuse6.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                lciPerson6.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            }
            else
            {
                lciDate6.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                lciAgree6.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                lciRefuse6.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                lciPerson6.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            }
        }
        #endregion

        private void GetListRequest()
        {
            DataTable m_tblDepartmentManage = new DataTable();

            m_tblTable = new DataTable();
            if (type == "ChuyenKhoaNganh")
            {
                m_tblTable = this.m_data.m_DCReadOnly.Log_Tranfers.GetListTranferForCensorship(m_data.m_DCReadOnly.Training_TrainingPrograms,
                    m_data.m_DCReadOnly.System_Users,
                    m_data.m_DCReadOnly.Personnel_Personnels,
                    m_data.m_DCReadOnly.Catalogue_Majors).ToDataTable();

                goto addToGridview;
            }
            if (type == "BuocThoiHoc")
            {
                m_tblTable = m_data.m_DCReadOnly.Log_ForceCanceleds.GetListForceCancelForCensorship(m_data.m_DCReadOnly.Catalogue_Majors,
                    m_data.m_DCReadOnly.Personnel_Personnels).ToDataTable();
                goto addToGridview;
            }
            if (type == "SuaDiem")
            {
                m_tblTable = m_data.m_DCReadOnly.Log_ScoreEdits.GetListScoreEditForCensorship(m_data.m_DCReadOnly.Training_TrainingPrograms,
                    m_data.m_DCReadOnly.Catalogue_Majors,
                    m_data.m_DCReadOnly.Personnel_Personnels).ToDataTable();
                goto addToGridview;
            }

            //// Thêm mới tại đây
            //if (type == "...")
            //{
            //    m_tblTable = m_data.m_DCReadOnly...
            //    goto addToGridview;
            //}

        addToGridview:
            if (m_tblTable.Rows.Count > 0)
            {
                gctView.DataSource = m_tblTable;
                grvView.ExpandAllGroups();
            }
            else
                gctView.DataSource = null;
        }

        private void InitalGridView()
        {
            if (type == "ChuyenKhoaNganh")
            {
                gcllogKey = "Log_TranferKey";
                gcldepartmentKey = "Catalogue_DepartmentKey";
                gclrequireUser = "RequireUser";
            }
            if (type == "BuocThoiHoc")
            {
                gcllogKey = "Log_ForceCanceledKey";
                gcldepartmentKey = "Catalogue_DepartmentKey";
                gclrequireUser = "RequireUser";
            }
            if (type == "SuaDiem")
            {
                gcllogKey = "Log_ScoreEditKey";
                gcldepartmentKey = "Catalogue_DepartmentKey";
                gclrequireUser = "RequireUser";
            }
            // Thêm mới tại đây
            //if (type == "")
            //{
            //    gcllogKey = "";
            //    gcldepartmentKey = "";
            //    gclrequireUser = "";
            //}

            DevExpress.XtraGrid.Columns.GridColumn gcl;
            gcl = new DevExpress.XtraGrid.Columns.GridColumn();
            grvView.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { gcl });
            gcl.Caption = "Khoa / Ban";
            gcl.Name = gcldepartmentKey;
            gcl.FieldName = gcldepartmentKey;
            gcl.Visible = true;
            gcl.VisibleIndex = 0;
            gcl.ColumnEdit = rluCatalogue_Department;
            gcl.AppearanceCell.Font = new System.Drawing.Font("Times New Roman", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            gcl.AppearanceCell.Options.UseFont = true;
            gcl.AppearanceHeader.Font = new System.Drawing.Font("Times New Roman", 9F);
            gcl.AppearanceHeader.Options.UseFont = true;
            gcl.GroupIndex = 0;

            gcl = new DevExpress.XtraGrid.Columns.GridColumn();
            grvView.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { gcl });
            gcl.Caption = "Mã phiếu";
            gcl.Name = gcllogKey;
            gcl.FieldName = gcllogKey;
            gcl.Visible = true;
            gcl.VisibleIndex = 0;
            gcl.AppearanceCell.Font = new System.Drawing.Font("Times New Roman", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            gcl.AppearanceCell.Options.UseFont = true;
            gcl.AppearanceHeader.Font = new System.Drawing.Font("Times New Roman", 9F);
            gcl.AppearanceHeader.Options.UseFont = true;

            gcl = new DevExpress.XtraGrid.Columns.GridColumn();
            grvView.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { gcl });
            gcl.Caption = "Mã số";
            gcl.Name = "ID";
            gcl.FieldName = "ID";
            gcl.Visible = true;
            gcl.VisibleIndex = 1;
            gcl.AppearanceCell.Font = new System.Drawing.Font("Times New Roman", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            gcl.AppearanceCell.Options.UseFont = true;
            gcl.AppearanceHeader.Font = new System.Drawing.Font("Times New Roman", 9F);
            gcl.AppearanceHeader.Options.UseFont = true;

            gcl = new DevExpress.XtraGrid.Columns.GridColumn();
            grvView.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { gcl });
            gcl.Caption = "Người yêu cầu";
            gcl.Name = gclrequireUser;
            gcl.FieldName = gclrequireUser;
            gcl.Visible = true;
            gcl.VisibleIndex = 2;
            gcl.ColumnEdit = rluRequireUser;
            gcl.AppearanceCell.Font = new System.Drawing.Font("Times New Roman", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            gcl.AppearanceCell.Options.UseFont = true;
            gcl.AppearanceHeader.Font = new System.Drawing.Font("Times New Roman", 9F);
            gcl.AppearanceHeader.Options.UseFont = true;

            gcl = new DevExpress.XtraGrid.Columns.GridColumn();
            grvView.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { gcl });
            gcl.Caption = "Training_StudentKey";
            gcl.Name = "Training_StudentKey";
            gcl.FieldName = "Training_StudentKey";
            gcl.Visible = false;
            gcl.VisibleIndex = -1;
            gcl.AppearanceCell.Font = new System.Drawing.Font("Times New Roman", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            gcl.AppearanceCell.Options.UseFont = true;
            gcl.AppearanceHeader.Font = new System.Drawing.Font("Times New Roman", 9F);
            gcl.AppearanceHeader.Options.UseFont = true;

            gcl = new DevExpress.XtraGrid.Columns.GridColumn();
            grvView.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { gcl });
            gcl.Caption = "Status";
            gcl.Name = "Status";
            gcl.FieldName = "Status";
            gcl.Visible = false;
            gcl.VisibleIndex = -1;
            gcl.AppearanceCell.Font = new System.Drawing.Font("Times New Roman", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            gcl.AppearanceCell.Options.UseFont = true;
            gcl.AppearanceHeader.Font = new System.Drawing.Font("Times New Roman", 9F);
            gcl.AppearanceHeader.Options.UseFont = true;
        }

        private void gctView_Click(object sender, EventArgs e)
        {
            if ((!grvView.IsFilterRow(grvView.FocusedRowHandle)) && (!grvView.IsGroupRow(grvView.FocusedRowHandle)))
            {
                m_iLogKey = int.Parse(grvView.GetFocusedRowCellValue(gcllogKey).ToString());
                m_iDepartmentKey = int.Parse(grvView.GetFocusedRowCellValue(gcldepartmentKey).ToString());
                ShowDisplay(true);
                studentInfo = new Training_StudentInfo(grvView.GetRowCellValue(grvView.FocusedRowHandle, "Training_StudentKey").ToString());
                if (type == "ChuyenKhoaNganh")
                {
                    tranferInfo = new Log_TranferInfo(int.Parse(grvView.GetFocusedRowCellValue(gcllogKey).ToString()));
                    ShowInformationMajorCourse();
                    if (tranferInfo.Properties.Status != null)
                        m_bRequireStatus = true;
                    else
                        m_bRequireStatus = false;
                }
                if (type == "BuocThoiHoc")
                {
                    forceCancelInfo = new Log_ForceCanceledInfo(int.Parse(grvView.GetFocusedRowCellValue(gcllogKey).ToString()));
                    ShowInformationForceCanceled();
                    if (forceCancelInfo.Properties.Status != null)
                        m_bRequireStatus = true;
                    else
                        m_bRequireStatus = false;
                }
                if (type == "SuaDiem")
                {
                    scoreInfo = new Log_ScoreEditInfo(int.Parse(grvView.GetFocusedRowCellValue(gcllogKey).ToString()));
                    ShowInfomationScoreEdit();
                    if (scoreInfo.Properties.Status != null)
                        m_bRequireStatus = true;
                    else
                        m_bRequireStatus = false;
                }
                //// Thêm mới tại đây
                //if (type == "SuaDiem")
                //{

                //}

                GetListCensorship(m_iDepartmentKey);
            }
        }

        private void ShowInformationMajorCourse()
        {
            lbcStudentName.Text = studentInfo.Properties.LastName + " " + studentInfo.Properties.SureName;

            gluTraining_TrainingProgram1.EditValue = tranferInfo.Properties.Training_TrainingProgramKeyBefore;
            programeInfo = new Training_TrainingProgramInfo(int.Parse(tranferInfo.Properties.Training_TrainingProgramKeyBefore.ToString()));
            gluCatalogue_Department1.EditValue = m_data.m_DCReadOnly.Catalogue_Departments.ReturnDepartmentKeyWithMajorKey(m_data.m_DCReadOnly.Catalogue_Majors,
                int.Parse(programeInfo.Properties.Catalogue_MajorKey.ToString()));
            gluCatalogue_Major1.EditValue = programeInfo.Properties.Catalogue_MajorKey;
            gluCatalogue_Level1.EditValue = programeInfo.Properties.Catalogue_LevelKey;
            gluCatalogue_TrainingType1.EditValue = programeInfo.Properties.Catalogue_TrainingTypeKey;
            gluCatalogue_Course1.EditValue = programeInfo.Properties.Catalogue_CourseKey;
            gluTraining_Class1.EditValue = int.Parse(tranferInfo.Properties.ClassBefore.ToString());
            txtStudentID1.Text = tranferInfo.Properties.BeforeID;

            gluTraining_TrainingProgram2.EditValue = tranferInfo.Properties.Training_TrainingProgramKeyAfter;
            programeInfo = new Training_TrainingProgramInfo(int.Parse(tranferInfo.Properties.Training_TrainingProgramKeyAfter.ToString()));
            gluCatalogue_Department2.EditValue = m_data.m_DCReadOnly.Catalogue_Departments.ReturnDepartmentKeyWithMajorKey(m_data.m_DCReadOnly.Catalogue_Majors,
                int.Parse(programeInfo.Properties.Catalogue_MajorKey.ToString()));
            gluCatalogue_Major2.EditValue = programeInfo.Properties.Catalogue_MajorKey;
            gluCatalogue_Level2.EditValue = programeInfo.Properties.Catalogue_LevelKey;
            gluCatalogue_TrainingType2.EditValue = programeInfo.Properties.Catalogue_TrainingTypeKey;
            gluCatalogue_Course2.EditValue = programeInfo.Properties.Catalogue_CourseKey;
            gluTraining_Class2.EditValue = tranferInfo.Properties.ClassAfter;
            txtStudentID2.Text = tranferInfo.Properties.AfterID;
        }

        private void ShowInformationForceCanceled()
        {
            lbcStudentName.Text = studentInfo.Properties.LastName + " " + studentInfo.Properties.SureName;

            gluTraining_TrainingProgram1.EditValue = forceCancelInfo.Properties.Training_TrainingProgramKey;
            programeInfo = new Training_TrainingProgramInfo(int.Parse(forceCancelInfo.Properties.Training_TrainingProgramKey.ToString()));
            gluCatalogue_Department1.EditValue = m_data.m_DCReadOnly.Catalogue_Departments.ReturnDepartmentKeyWithMajorKey(m_data.m_DCReadOnly.Catalogue_Majors,
                int.Parse(programeInfo.Properties.Catalogue_MajorKey.ToString()));
            gluCatalogue_Major1.EditValue = programeInfo.Properties.Catalogue_MajorKey;
            gluCatalogue_Level1.EditValue = programeInfo.Properties.Catalogue_LevelKey;
            gluCatalogue_TrainingType1.EditValue = programeInfo.Properties.Catalogue_TrainingTypeKey;
            gluCatalogue_Course1.EditValue = programeInfo.Properties.Catalogue_CourseKey;
            stdClassInfo = new Training_Student_ClassInfo("nguyen.tuanan",
                studentInfo.Properties.Training_StudentKey.ToString(),
                int.Parse(programeInfo.Properties.Catalogue_LevelKey.ToString()),
                int.Parse(programeInfo.Properties.Catalogue_MajorKey.ToString()),
                int.Parse(programeInfo.Properties.Catalogue_TrainingTypeKey.ToString()),
                int.Parse(programeInfo.Properties.Catalogue_CourseKey.ToString()));
            gluTraining_Class1.EditValue = stdClassInfo.Properties.Training_ClassKey;
            txtStudentID1.Text = forceCancelInfo.Properties.ID;
            grc4.Visible = false;
        }

        private void ShowInfomationScoreEdit()
        {
            lbcStudentName.Text = studentInfo.Properties.LastName + " " + studentInfo.Properties.SureName;

            gluTraining_TrainingProgram1.EditValue = scoreInfo.Properties.Training_TrainingProgramKey;
            programeInfo = new Training_TrainingProgramInfo(int.Parse(scoreInfo.Properties.Training_TrainingProgramKey.ToString()));
            gluCatalogue_Department1.EditValue = m_data.m_DCReadOnly.Catalogue_Departments.ReturnDepartmentKeyWithMajorKey(m_data.m_DCReadOnly.Catalogue_Majors,
                int.Parse(programeInfo.Properties.Catalogue_MajorKey.ToString()));
            gluCatalogue_Major1.EditValue = programeInfo.Properties.Catalogue_MajorKey;
            gluCatalogue_Level1.EditValue = programeInfo.Properties.Catalogue_LevelKey;
            gluCatalogue_TrainingType1.EditValue = programeInfo.Properties.Catalogue_TrainingTypeKey;
            gluCatalogue_Course1.EditValue = programeInfo.Properties.Catalogue_CourseKey;
            stdClassInfo = new Training_Student_ClassInfo("nguyen.tuanan",
                studentInfo.Properties.Training_StudentKey.ToString(),
                int.Parse(programeInfo.Properties.Catalogue_LevelKey.ToString()),
                int.Parse(programeInfo.Properties.Catalogue_MajorKey.ToString()),
                int.Parse(programeInfo.Properties.Catalogue_TrainingTypeKey.ToString()),
                int.Parse(programeInfo.Properties.Catalogue_CourseKey.ToString()));
            gluTraining_Class1.EditValue = int.Parse(stdClassInfo.Properties.Training_ClassKey.ToString());
            txtStudentID1.Text = stdClassInfo.Properties.ID;
            grc4.Visible = false;
        }

        // Thêm hàm hiển thị mới ở đây private void ShowInfomation...()


        private void GetListCensorship(int m_iDepartmentKey)
        {
            m_tblCensorship = new DataTable();
            m_tblTable = new DataTable();
            m_tblTable = m_data.m_DCReadOnly.Leaderships.GetLeadershipRelationshipKeyWithDepartmentKey(m_iDepartmentKey).ToDataTable();
            if (m_tblTable.Rows.Count > 0)
                AddCensorship(m_tblTable);
            else
            {
                ShowCensorshipAll(false);
                m_tblCensorship = new DataTable();
            }
        }

        private void AddCensorship(DataTable m_tbl)
        {
            List<int> listKey = new List<int>();
            for (int i = 0; i < m_tbl.Rows.Count; i++)
                listKey.Add(m_tbl.Rows[i]["LeadershipRelationshipKey"].ToInt());
            m_tblCensorship = m_data.m_DCReadOnly.LeadershipRelationships.GetListCensorshipWithLeadershipRelationshipKey(m_data.m_DCReadOnly.Personnel_Personnels,
                listKey).ToDataTable();

            // Hiển thị trên giao diện
            ShowCensorshipAll(false);
            if (m_tblCensorship.Rows.Count > 0)
                SetControlAndValues(m_tblCensorship);
        }

        private void SetControlAndValues(DataTable m_tblCensorship)
        {
            if (m_tblCensorship.Rows.Count == 1)
            {
                ShowCensorship1(true);
                DisplayCensorship((DataRow)m_tblCensorship.Rows[0], 1);
                return;
            }
            if (m_tblCensorship.Rows.Count == 2)
            {
                ShowCensorship1(true);
                ShowCensorship4(true);
                DisplayCensorship((DataRow)m_tblCensorship.Rows[0], 1);
                DisplayCensorship((DataRow)m_tblCensorship.Rows[1], 4);
                return;
            }
            if (m_tblCensorship.Rows.Count == 3)
            {
                ShowCensorship1(true);
                ShowCensorship4(true);
                ShowCensorship2(true);
                DisplayCensorship((DataRow)m_tblCensorship.Rows[0], 1);
                DisplayCensorship((DataRow)m_tblCensorship.Rows[1], 4);
                DisplayCensorship((DataRow)m_tblCensorship.Rows[2], 2);
                return;
            }
            if (m_tblCensorship.Rows.Count == 4)
            {
                ShowCensorship1(true);
                ShowCensorship4(true);
                ShowCensorship2(true);
                ShowCensorship5(true);
                DisplayCensorship((DataRow)m_tblCensorship.Rows[0], 1);
                DisplayCensorship((DataRow)m_tblCensorship.Rows[1], 4);
                DisplayCensorship((DataRow)m_tblCensorship.Rows[2], 2);
                DisplayCensorship((DataRow)m_tblCensorship.Rows[3], 5);
                return;
            }
            if (m_tblCensorship.Rows.Count == 5)
            {
                ShowCensorship1(true);
                ShowCensorship4(true);
                ShowCensorship2(true);
                ShowCensorship5(true);
                ShowCensorship3(true);
                DisplayCensorship((DataRow)m_tblCensorship.Rows[0], 1);
                DisplayCensorship((DataRow)m_tblCensorship.Rows[1], 4);
                DisplayCensorship((DataRow)m_tblCensorship.Rows[2], 2);
                DisplayCensorship((DataRow)m_tblCensorship.Rows[3], 5);
                DisplayCensorship((DataRow)m_tblCensorship.Rows[4], 3);
                return;
            }
            if (m_tblCensorship.Rows.Count == 6)
            {
                ShowCensorship1(true);
                ShowCensorship4(true);
                ShowCensorship2(true);
                ShowCensorship5(true);
                ShowCensorship3(true);
                ShowCensorship6(true);
                DisplayCensorship((DataRow)m_tblCensorship.Rows[0], 1);
                DisplayCensorship((DataRow)m_tblCensorship.Rows[1], 4);
                DisplayCensorship((DataRow)m_tblCensorship.Rows[2], 2);
                DisplayCensorship((DataRow)m_tblCensorship.Rows[3], 5);
                DisplayCensorship((DataRow)m_tblCensorship.Rows[4], 3);
                DisplayCensorship((DataRow)m_tblCensorship.Rows[5], 6);
                return;
            }
        }

        private void DisplayCensorship(DataRow row, int local)
        {
            Personnel_PersonnelInfo personinfo = new Personnel_PersonnelInfo(Guid.Parse(row["Personnel_PersonnelKey"].ToString()));
            Leader_CensorshipInfo censorInfo;
            censorInfo = new Leader_CensorshipInfo(int.Parse(row["LeadershipRelationshipKey"].ToString()), m_iLogKey, typeRule);
            if (local == 1)
            {
                lbcPersonnal_1.Text = personinfo.Properties.LastName + " " + personinfo.Properties.Name;
                if (censorInfo.Properties.Datetime == null)
                    lbcDatetime_1.Text = "Ngày    ...    tháng    ...    năm    ... ";
                else
                {
                    DateTime m_datetime = Convert.ToDateTime(censorInfo.Properties.Datetime);
                    lbcDatetime_1.Text = "Ngày    " + m_datetime.Day + "    tháng    " + m_datetime.Month + "    năm    " + m_datetime.Year;
                }
                if (censorInfo.Properties.Status == null)
                {
                    ckeAgree1.Checked = false;
                    ckeRefuse1.Checked = false;
                }
                else
                {
                    if (censorInfo.Properties.Status == true)
                    {
                        ckeAgree1.Checked = true;
                        ckeRefuse1.Checked = false;
                    }
                    else
                    {
                        ckeAgree1.Checked = false;
                        ckeRefuse1.Checked = true;
                    }
                }
                if (personinfo.Properties.Personnel_PersonnelKey.ToString() == Utils.LoginInfo.GblLoginInfo.sysLoginPersionalKey.ToString() && m_bRequireStatus == false)
                    Enable1(true);
                else
                    Enable1(false);
            }
            if (local == 2)
            {
                lbcPersonnal_2.Text = personinfo.Properties.LastName + " " + personinfo.Properties.Name;
                if (censorInfo.Properties.Datetime == null)
                    lbcDatetime_2.Text = "Ngày    ...    tháng    ...    năm    ... ";
                else
                {
                    DateTime m_datetime = Convert.ToDateTime(censorInfo.Properties.Datetime);
                    lbcDatetime_2.Text = "Ngày    " + m_datetime.Day + "    tháng    " + m_datetime.Month + "    năm    " + m_datetime.Year;
                }
                if (censorInfo.Properties.Status == null)
                {
                    ckeAgree2.Checked = false;
                    ckeRefuse2.Checked = false;
                }
                else
                {
                    if (censorInfo.Properties.Status == true)
                    {
                        ckeAgree2.Checked = true;
                        ckeRefuse2.Checked = false;
                    }
                    else
                    {
                        ckeAgree2.Checked = false;
                        ckeRefuse2.Checked = true;
                    }
                }
                if (personinfo.Properties.Personnel_PersonnelKey.ToString() == Utils.LoginInfo.GblLoginInfo.sysLoginPersionalKey.ToString() && m_bRequireStatus == false)
                    Enable2(true);
                else
                    Enable2(false);
            }
            if (local == 3)
            {
                lbcPersonnal_3.Text = personinfo.Properties.LastName + " " + personinfo.Properties.Name;
                if (censorInfo.Properties.Datetime == null)
                    lbcDatetime_3.Text = "Ngày    ...    tháng    ...    năm    ... ";
                else
                {
                    DateTime m_datetime = Convert.ToDateTime(censorInfo.Properties.Datetime);
                    lbcDatetime_3.Text = "Ngày    " + m_datetime.Day + "    tháng    " + m_datetime.Month + "    năm    " + m_datetime.Year;
                }
                if (censorInfo.Properties.Status == null)
                {
                    ckeAgree3.Checked = false;
                    ckeRefuse3.Checked = false;
                }
                else
                {
                    if (censorInfo.Properties.Status == true)
                    {
                        ckeAgree3.Checked = true;
                        ckeRefuse3.Checked = false;
                    }
                    else
                    {
                        ckeAgree3.Checked = false;
                        ckeRefuse3.Checked = true;
                    }
                }
                if (personinfo.Properties.Personnel_PersonnelKey.ToString() == Utils.LoginInfo.GblLoginInfo.sysLoginPersionalKey.ToString() && m_bRequireStatus == false)
                    Enable3(true);
                else
                    Enable3(false);
            }
            if (local == 4)
            {
                lbcPersonnal_4.Text = personinfo.Properties.LastName + " " + personinfo.Properties.Name;
                if (censorInfo.Properties.Datetime == null)
                    lbcDatetime_4.Text = "Ngày    ...    tháng    ...    năm    ... ";
                else
                {
                    DateTime m_datetime = Convert.ToDateTime(censorInfo.Properties.Datetime);
                    lbcDatetime_4.Text = "Ngày    " + m_datetime.Day + "    tháng    " + m_datetime.Month + "    năm    " + m_datetime.Year;
                }
                if (censorInfo.Properties.Status == null)
                {
                    ckeAgree4.Checked = false;
                    ckeRefuse4.Checked = false;
                }
                else
                {
                    if (censorInfo.Properties.Status == true)
                    {
                        ckeAgree4.Checked = true;
                        ckeRefuse4.Checked = false;
                    }
                    else
                    {
                        ckeAgree4.Checked = false;
                        ckeRefuse4.Checked = true;
                    }
                }
                if (personinfo.Properties.Personnel_PersonnelKey.ToString() == Utils.LoginInfo.GblLoginInfo.sysLoginPersionalKey.ToString() && m_bRequireStatus == false)
                    Enable4(true);
                else
                    Enable4(false);
            }
            if (local == 5)
            {
                lbcPersonnal_5.Text = personinfo.Properties.LastName + " " + personinfo.Properties.Name;
                if (censorInfo.Properties.Datetime == null)
                    lbcDatetime_5.Text = "Ngày    ...    tháng    ...    năm    ... ";
                else
                {
                    DateTime m_datetime = Convert.ToDateTime(censorInfo.Properties.Datetime);
                    lbcDatetime_5.Text = "Ngày    " + m_datetime.Day + "    tháng    " + m_datetime.Month + "    năm    " + m_datetime.Year;
                }
                if (censorInfo.Properties.Status == null)
                {
                    ckeAgree5.Checked = false;
                    ckeRefuse5.Checked = false;
                }
                else
                {
                    if (censorInfo.Properties.Status == true)
                    {
                        ckeAgree5.Checked = true;
                        ckeRefuse5.Checked = false;
                    }
                    else
                    {
                        ckeAgree5.Checked = false;
                        ckeRefuse5.Checked = true;
                    }
                }
                if (personinfo.Properties.Personnel_PersonnelKey.ToString() == Utils.LoginInfo.GblLoginInfo.sysLoginPersionalKey.ToString() && m_bRequireStatus == false)
                    Enable5(true);
                else
                    Enable5(false);
            }
            if (local == 6)
            {
                lbcPersonnal_6.Text = personinfo.Properties.LastName + " " + personinfo.Properties.Name;
                if (censorInfo.Properties.Datetime == null)
                    lbcDatetime_6.Text = "Ngày    ...    tháng    ...    năm    ... ";
                else
                {
                    DateTime m_datetime = Convert.ToDateTime(censorInfo.Properties.Datetime);
                    lbcDatetime_6.Text = "Ngày    " + m_datetime.Day + "    tháng    " + m_datetime.Month + "    năm    " + m_datetime.Year;
                }
                if (censorInfo.Properties.Status == null)
                {
                    ckeAgree6.Checked = false;
                    ckeRefuse6.Checked = false;
                }
                else
                {
                    if (censorInfo.Properties.Status == true)
                    {
                        ckeAgree6.Checked = true;
                        ckeRefuse6.Checked = false;
                    }
                    else
                    {
                        ckeAgree6.Checked = false;
                        ckeRefuse6.Checked = true;
                    }
                }
            }
            if (personinfo.Properties.Personnel_PersonnelKey.ToString() == Utils.LoginInfo.GblLoginInfo.sysLoginPersionalKey.ToString() && m_bRequireStatus == false)
                Enable6(true);
            else
                Enable6(false);

            GetChecked();
        }

        #region [Enable]
        private void EnableAll(bool value)
        {
            if (value)
            {
                Enable1(true);
                Enable2(true);
                Enable3(true);
                Enable4(true);
                Enable5(true);
                Enable6(true);
            }
            else
            {
                Enable1(false);
                Enable2(false);
                Enable3(false);
                Enable4(false);
                Enable5(false);
                Enable6(false);
            }
        }

        private void Enable1(bool value)
        {
            if (value)
            {
                ckeAgree1.Enabled = true;
                ckeRefuse1.Enabled = true;
            }
            else
            {
                ckeAgree1.Enabled = false;
                ckeRefuse1.Enabled = false;
            }
        }

        private void Enable2(bool value)
        {
            if (value)
            {
                ckeAgree2.Enabled = true;
                ckeRefuse2.Enabled = true;
            }
            else
            {
                ckeAgree2.Enabled = false;
                ckeRefuse2.Enabled = false;
            }
        }

        private void Enable3(bool value)
        {
            if (value)
            {
                ckeAgree3.Enabled = true;
                ckeRefuse3.Enabled = true;
            }
            else
            {
                ckeAgree3.Enabled = false;
                ckeRefuse3.Enabled = false;
            }
        }

        private void Enable4(bool value)
        {
            if (value)
            {
                ckeAgree4.Enabled = true;
                ckeRefuse4.Enabled = true;
            }
            else
            {
                ckeAgree4.Enabled = false;
                ckeRefuse4.Enabled = false;
            }
        }

        private void Enable5(bool value)
        {
            if (value)
            {
                ckeAgree5.Enabled = true;
                ckeRefuse5.Enabled = true;
            }
            else
            {
                ckeAgree5.Enabled = false;
                ckeRefuse5.Enabled = false;
            }
        }

        private void Enable6(bool value)
        {
            if (value)
            {
                ckeAgree6.Enabled = true;
                ckeRefuse6.Enabled = true;
            }
            else
            {
                ckeAgree6.Enabled = false;
                ckeRefuse6.Enabled = false;
            }
        }
        #endregion

        private void GetChecked()
        {
            if (lciAgree1.Visibility != DevExpress.XtraLayout.Utils.LayoutVisibility.Never)
            {
                if (ckeAgree1.Checked)
                    value1 = 1;
                else
                    if (ckeRefuse1.Checked)
                        value1 = 2;
                    else
                        value1 = 0;
            }
            if (lciAgree2.Visibility != DevExpress.XtraLayout.Utils.LayoutVisibility.Never)
            {
                if (ckeAgree2.Checked)
                    value2 = 1;
                else
                    if (ckeRefuse2.Checked)
                        value2 = 2;
                    else
                        value2 = 0;
            }
            if (lciAgree3.Visibility != DevExpress.XtraLayout.Utils.LayoutVisibility.Never)
            {
                if (ckeAgree3.Checked)
                    value3 = 1;
                else
                    if (ckeRefuse3.Checked)
                        value3 = 2;
                    else
                        value3 = 0;
            }
            if (lciAgree4.Visibility != DevExpress.XtraLayout.Utils.LayoutVisibility.Never)
            {
                if (ckeAgree4.Checked)
                    value4 = 1;
                else
                    if (ckeRefuse4.Checked)
                        value4 = 2;
                    else
                        value4 = 0;
            }
            if (lciAgree5.Visibility != DevExpress.XtraLayout.Utils.LayoutVisibility.Never)
            {
                if (ckeAgree5.Checked)
                    value5 = 1;
                else
                    if (ckeRefuse5.Checked)
                        value5 = 2;
                    else
                        value5 = 0;
            }
            if (lciAgree6.Visibility != DevExpress.XtraLayout.Utils.LayoutVisibility.Never)
            {
                if (ckeAgree6.Checked)
                    value6 = 1;
                else
                    if (ckeRefuse6.Checked)
                        value6 = 2;
                    else
                        value6 = 0;
            }
        }

        #region [Checked Click]
        private void ckeAgree1_Click(object sender, EventArgs e)
        {
            if (_flagEdit)
            {
                if (ckeAgree1.Checked)
                    ckeRefuse1.Checked = false;
            }
        }

        private void ckeRefuse1_Click(object sender, EventArgs e)
        {
            if (_flagEdit)
            {
                if (ckeRefuse1.Checked)
                    ckeAgree1.Checked = false;
            }
        }

        private void ckeAgree2_Click(object sender, EventArgs e)
        {
            if (_flagEdit)
            {
                if (ckeAgree2.Checked)
                    ckeRefuse2.Checked = false;
            }
        }

        private void ckeRefuse2_Click(object sender, EventArgs e)
        {
            if (_flagEdit)
            {
                if (ckeRefuse2.Checked)
                    ckeAgree2.Checked = false;
            }
        }

        private void ckeAgree3_Click(object sender, EventArgs e)
        {
            if (_flagEdit)
            {

                if (ckeAgree3.Checked)
                    ckeRefuse3.Checked = false;
            }
        }

        private void ckeRefuse3_Click(object sender, EventArgs e)
        {
            if (_flagEdit)
            {

                if (ckeRefuse3.Checked)
                    ckeAgree3.Checked = false;
            }
        }

        private void ckeAgree4_Click(object sender, EventArgs e)
        {
            if (_flagEdit)
            {

                if (ckeAgree4.Checked)
                    ckeRefuse4.Checked = false;
            }
        }

        private void ckeRefuse4_Click(object sender, EventArgs e)
        {
            if (_flagEdit)
            {

                if (ckeRefuse4.Checked)
                    ckeAgree4.Checked = false;
            }
        }

        private void ckeAgree5_Click(object sender, EventArgs e)
        {
            if (_flagEdit)
            {

                if (ckeAgree5.Checked)
                    ckeRefuse5.Checked = false;
            }
        }

        private void ckeRefuse5_Click(object sender, EventArgs e)
        {
            if (_flagEdit)
            {

                if (ckeRefuse5.Checked)
                    ckeAgree5.Checked = false;
            }
        }

        private void ckeAgree6_Click(object sender, EventArgs e)
        {
            if (_flagEdit)
            {

                if (ckeAgree6.Checked)
                    ckeRefuse6.Checked = false;
            }
        }

        private void ckeRefuse6_Click(object sender, EventArgs e)
        {
            if (_flagEdit)
            {

                if (ckeRefuse6.Checked)
                    ckeAgree6.Checked = false;
            }
        }
        #endregion

        private void xucBar1_btnCancel_Click(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            GetListCensorship(m_iDepartmentKey);
        }

        private void xucBar1_btnPrint_Click(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string strTenlop = "";// gluTraining_Student.Text.ToUpper();
            this.printableComponentLink1.ClearDocument();
            this.printableComponentLink1.RtfReportHeader = this.printableComponentLink1.RtfReportHeader.Replace("sinhvien", UniMana.Utils.Helper.String.GetRtfUnicodeEscapedString(strTenlop));
            string ngaythang = "Tân An," + " ngày" + " " + DateTime.Now.Day + " " + "tháng" + " " + DateTime.Now.Month + " " + "năm" + " " + DateTime.Now.Year;
            string nguoidung = SysGlobalVar.g_sta_sysUserLogin.Properties.FullName;
            this.printableComponentLink1.RtfReportFooter = this.printableComponentLink1.RtfReportFooter.Replace("nguoilap", UniMana.Utils.Helper.String.GetRtfUnicodeEscapedString(nguoidung));
            this.printableComponentLink1.RtfReportFooter = this.printableComponentLink1.RtfReportFooter.Replace("ngaythang", UniMana.Utils.Helper.String.GetRtfUnicodeEscapedString(ngaythang));
            printableComponentLink1.ShowPreview();
        }

        private void xucBar1_btnSave_Click(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            xucBar1.dlg.Close();
            DialogResult result = XtraMessageBox.Show("Bạn có muốn lưu thay đổi", "Thông báo", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                GetChecked();
                #region [Xác định vị trí lưu phê duyệt]
                if ((lciAgree1.Visibility != DevExpress.XtraLayout.Utils.LayoutVisibility.Never) && (ckeAgree1.Enabled))
                {
                    Leader_CensorshipInfo cenInfo = new Leader_CensorshipInfo(int.Parse(m_tblCensorship.Rows[0]["LeadershipRelationshipKey"].ToString()), m_iLogKey, typeRule, Utils.LoginInfo.GblLoginInfo.sysLoginPersionalKey.ToString());
                    cenInfo.Properties.LogKey = m_iLogKey;
                    cenInfo.Properties.Catalogue_RuleNameViKey = typeRule;
                    cenInfo.Properties.LeadershipRelationshipKey = int.Parse(m_tblCensorship.Rows[0]["LeadershipRelationshipKey"].ToString());
                    cenInfo.Properties.Personnel_PersonnelKey = Utils.LoginInfo.GblLoginInfo.sysLoginPersionalKey;
                    cenInfo.Properties.Datetime = SysDateTime.Now;
                    SetStatus(ref cenInfo, value1);
                    cenInfo.Save();
                }
                if ((lciAgree2.Visibility != DevExpress.XtraLayout.Utils.LayoutVisibility.Never) && (ckeAgree2.Enabled))
                {
                    Leader_CensorshipInfo cenInfo = new Leader_CensorshipInfo(int.Parse(m_tblCensorship.Rows[2]["LeadershipRelationshipKey"].ToString()), m_iLogKey, typeRule, Utils.LoginInfo.GblLoginInfo.sysLoginPersionalKey.ToString());
                    cenInfo.Properties.LogKey = m_iLogKey;
                    cenInfo.Properties.Catalogue_RuleNameViKey = typeRule;
                    cenInfo.Properties.LeadershipRelationshipKey = int.Parse(m_tblCensorship.Rows[2]["LeadershipRelationshipKey"].ToString());
                    cenInfo.Properties.Personnel_PersonnelKey = Utils.LoginInfo.GblLoginInfo.sysLoginPersionalKey;
                    cenInfo.Properties.Datetime = SysDateTime.Now;
                    SetStatus(ref cenInfo, value2);
                    cenInfo.Save();
                }
                if ((lciAgree3.Visibility != DevExpress.XtraLayout.Utils.LayoutVisibility.Never) && (ckeAgree3.Enabled))
                {
                    Leader_CensorshipInfo cenInfo = new Leader_CensorshipInfo(int.Parse(m_tblCensorship.Rows[4]["LeadershipRelationshipKey"].ToString()), m_iLogKey, typeRule, Utils.LoginInfo.GblLoginInfo.sysLoginPersionalKey.ToString());
                    cenInfo.Properties.LogKey = m_iLogKey;
                    cenInfo.Properties.Catalogue_RuleNameViKey = typeRule;
                    cenInfo.Properties.LeadershipRelationshipKey = int.Parse(m_tblCensorship.Rows[4]["LeadershipRelationshipKey"].ToString());
                    cenInfo.Properties.Personnel_PersonnelKey = Utils.LoginInfo.GblLoginInfo.sysLoginPersionalKey;
                    cenInfo.Properties.Datetime = SysDateTime.Now;
                    SetStatus(ref cenInfo, value3);
                    cenInfo.Save();
                }
                if ((lciAgree4.Visibility != DevExpress.XtraLayout.Utils.LayoutVisibility.Never) && (ckeAgree4.Enabled))
                {
                    Leader_CensorshipInfo cenInfo = new Leader_CensorshipInfo(int.Parse(m_tblCensorship.Rows[1]["LeadershipRelationshipKey"].ToString()), m_iLogKey, typeRule, Utils.LoginInfo.GblLoginInfo.sysLoginPersionalKey.ToString());
                    cenInfo.Properties.LogKey = m_iLogKey;
                    cenInfo.Properties.Catalogue_RuleNameViKey = typeRule;
                    cenInfo.Properties.LeadershipRelationshipKey = int.Parse(m_tblCensorship.Rows[1]["LeadershipRelationshipKey"].ToString());
                    cenInfo.Properties.Personnel_PersonnelKey = Utils.LoginInfo.GblLoginInfo.sysLoginPersionalKey;
                    cenInfo.Properties.Datetime = SysDateTime.Now;
                    SetStatus(ref cenInfo, value4);
                    cenInfo.Save();
                }
                if ((lciAgree5.Visibility != DevExpress.XtraLayout.Utils.LayoutVisibility.Never) && (ckeAgree5.Enabled))
                {
                    Leader_CensorshipInfo cenInfo = new Leader_CensorshipInfo(int.Parse(m_tblCensorship.Rows[3]["LeadershipRelationshipKey"].ToString()), m_iLogKey, typeRule, Utils.LoginInfo.GblLoginInfo.sysLoginPersionalKey.ToString());
                    cenInfo.Properties.LogKey = m_iLogKey;
                    cenInfo.Properties.Catalogue_RuleNameViKey = typeRule;
                    cenInfo.Properties.LeadershipRelationshipKey = int.Parse(m_tblCensorship.Rows[3]["LeadershipRelationshipKey"].ToString());
                    cenInfo.Properties.Personnel_PersonnelKey = Utils.LoginInfo.GblLoginInfo.sysLoginPersionalKey;
                    cenInfo.Properties.Datetime = SysDateTime.Now;
                    SetStatus(ref cenInfo, value5);
                    cenInfo.Save();
                }
                if ((lciAgree6.Visibility != DevExpress.XtraLayout.Utils.LayoutVisibility.Never) && (ckeAgree6.Enabled))
                {
                    Leader_CensorshipInfo cenInfo = new Leader_CensorshipInfo(int.Parse(m_tblCensorship.Rows[5]["LeadershipRelationshipKey"].ToString()), m_iLogKey, typeRule, Utils.LoginInfo.GblLoginInfo.sysLoginPersionalKey.ToString());
                    cenInfo.Properties.LogKey = m_iLogKey;
                    cenInfo.Properties.Catalogue_RuleNameViKey = typeRule;
                    cenInfo.Properties.LeadershipRelationshipKey = int.Parse(m_tblCensorship.Rows[5]["LeadershipRelationshipKey"].ToString());
                    cenInfo.Properties.Personnel_PersonnelKey = Utils.LoginInfo.GblLoginInfo.sysLoginPersionalKey;
                    cenInfo.Properties.Datetime = SysDateTime.Now;
                    SetStatus(ref cenInfo, value6);
                    cenInfo.Save();
                }
                #endregion
                // Reload lại danh sách lãnh đạo phê duyệt
                GetListCensorship(m_iDepartmentKey);

                if (type == "ChuyenKhoaNganh")
                    CheckUpdateTranfer();

                if (type == "BuocThoiHoc")
                    CheckUpdateForceCancel();

                if (type == "SuaDiem")
                    CheckUpdateScoreEdit();
            }
        }

        private bool UpdatePermit()
        {
            if (ckeAgree1.Enabled == true)
                return true;
            if (ckeAgree2.Enabled == true)
                return true;
            if (ckeAgree3.Enabled == true)
                return true;
            if (ckeAgree4.Enabled == true)
                return true;
            if (ckeAgree5.Enabled == true)
                return true;
            if (ckeAgree6.Enabled == true)
                return true;
            return false;
        }

        private void CheckUpdateTranfer()
        {
            if (UpdatePermit())
            {
                LeadershipRelationshipInfo info = new LeadershipRelationshipInfo(GblLoginInfo.sysLoginPersionalKey);
                Leader_CensorshipInfo cenInfo = new Leader_CensorshipInfo(info.Properties.LeadershipRelationshipKey,
                    tranferInfo.Properties.Log_TranferKey,
                    typeRule);
                if (info.Properties.ApproveLevel != null)
                    if (info.Properties.ApproveLevel == 0)
                    {
                        if (cenInfo.Properties.Status != null)
                        {
                            if (cenInfo.Properties.Status == true)
                            {
                                // Duyệt
                                DialogResult result = XtraMessageBox.Show("Bạn muốn phê duyệt phiếu " + tranferInfo.Properties.Log_TranferKey, "Thông báo", MessageBoxButtons.YesNo);
                                if (result == DialogResult.Yes)
                                {
                                    // Thêm chương trình đào tạo mới cho sinh viên
                                    Training_TrainingProgramInfo programinfo = new Training_TrainingProgramInfo(int.Parse(tranferInfo.Properties.Training_TrainingProgramKeyAfter.ToString()));
                                    Training_Student_ClassInfo trnclsinfo = new Training_Student_ClassInfo(tranferInfo.Properties.Training_StudentKey.ToString(),
                                        int.Parse(tranferInfo.Properties.Training_TrainingProgramKeyAfter.ToString()),
                                        true);
                                    if (trnclsinfo.Properties.Training_StudentKey.ToString() != "00000000-0000-0000-0000-000000000000")
                                    {
                                        trnclsinfo.Properties.Mainly = true;
                                        trnclsinfo.Save();
                                    }
                                    else
                                    {
                                        trnclsinfo.Properties.Training_StudentKey = Guid.Parse(tranferInfo.Properties.Training_StudentKey.ToString());
                                        trnclsinfo.Properties.Catalogue_CourseKey = int.Parse(programinfo.Properties.Catalogue_CourseKey.ToString());
                                        trnclsinfo.Properties.Catalogue_LevelKey = int.Parse(programinfo.Properties.Catalogue_LevelKey.ToString());
                                        trnclsinfo.Properties.Catalogue_MajorKey = int.Parse(programinfo.Properties.Catalogue_MajorKey.ToString());
                                        trnclsinfo.Properties.Catalogue_StatusKey = 1;
                                        trnclsinfo.Properties.Catalogue_TrainingTypeKey = int.Parse(programinfo.Properties.Catalogue_TrainingTypeKey.ToString());
                                        trnclsinfo.Properties.ID = tranferInfo.Properties.AfterID;
                                        trnclsinfo.Properties.Training_ClassKey = tranferInfo.Properties.ClassAfter;
                                        trnclsinfo.Properties.Training_TrainingProgramKey = tranferInfo.Properties.Training_TrainingProgramKeyAfter;
                                        trnclsinfo.Properties.Mainly = true;
                                        trnclsinfo.Save();
                                    }
                                    // Cập nhật trạng thái chương trình cũ
                                    trnclsinfo = new Training_Student_ClassInfo(tranferInfo.Properties.Training_StudentKey.ToString(),
                                        int.Parse(tranferInfo.Properties.Training_TrainingProgramKeyBefore.ToString()),
                                        true);
                                    trnclsinfo.Properties.Mainly = false;
                                    trnclsinfo.Save();
                                    // Ghi nhận phê duyệt
                                    tranferInfo.Properties.Status = true;
                                    tranferInfo.Save();
                                    GetListCensorship(m_iDepartmentKey);
                                }
                            }
                            else
                            {
                                // Không duyệt
                                DialogResult result = XtraMessageBox.Show("Bạn không muốn phê duyệt phiếu " + tranferInfo.Properties.Log_TranferKey, "Thông báo", MessageBoxButtons.YesNo);
                                if (result == DialogResult.Yes)
                                {
                                    tranferInfo.Properties.Status = false;
                                    tranferInfo.Save();
                                    GetListCensorship(m_iDepartmentKey);
                                }
                            }
                        }
                    }
            }
        }

        private void CheckUpdateForceCancel()
        {
            if (UpdatePermit())
            {
                LeadershipRelationshipInfo info = new LeadershipRelationshipInfo(GblLoginInfo.sysLoginPersionalKey);
                Leader_CensorshipInfo cenInfo = new Leader_CensorshipInfo(info.Properties.LeadershipRelationshipKey,
                    forceCancelInfo.Properties.Log_ForceCanceledKey,
                    typeRule);
                if (info.Properties.ApproveLevel != null)
                    if (info.Properties.ApproveLevel == 0)
                    {
                        if (cenInfo.Properties.Status != null)
                        {
                            if (cenInfo.Properties.Status == true)
                            {
                                DialogResult result = XtraMessageBox.Show("Bạn muốn phê duyệt phiếu " + m_iLogKey, "Thông báo", MessageBoxButtons.YesNo);
                                if (result == DialogResult.Yes)
                                {
                                    Training_Student_ClassInfo studentclass = new Training_Student_ClassInfo(studentInfo.Properties.Training_StudentKey.ToString(),
                                        int.Parse(forceCancelInfo.Properties.Training_TrainingProgramKey.ToString()),
                                        true);
                                    studentclass.Properties.Catalogue_StatusKey = 14;
                                    studentclass.Save();
                                    // Ghi nhận phê duyệt                                
                                    forceCancelInfo.Properties.Status = true;
                                    forceCancelInfo.Save();
                                    GetListCensorship(m_iDepartmentKey);
                                }
                            }
                            else
                            {
                                DialogResult result = XtraMessageBox.Show("Bạn không muốn phê duyệt phiếu " + m_iLogKey, "Thông báo", MessageBoxButtons.YesNo);
                                if (result == DialogResult.Yes)
                                {
                                    forceCancelInfo.Properties.Status = true;
                                    forceCancelInfo.Save();
                                    GetListCensorship(m_iDepartmentKey);
                                }
                            }
                        }
                    }
            }
        }

        private void CheckUpdateScoreEdit()
        {
            if (UpdatePermit())
            {
                LeadershipRelationshipInfo info = new LeadershipRelationshipInfo(GblLoginInfo.sysLoginPersionalKey);
                Leader_CensorshipInfo cenInfo = new Leader_CensorshipInfo(info.Properties.LeadershipRelationshipKey,
                    scoreInfo.Properties.Log_ScoreEditKey,
                    typeRule);
                if (info.Properties.ApproveLevel != null)
                    if (info.Properties.ApproveLevel == 0)
                    {
                        if (cenInfo.Properties.Status != null)
                        {
                            if (cenInfo.Properties.Status == true)
                            {
                                Log_ScoreEditInfo loginfo = new Log_ScoreEditInfo(m_iLogKey);
                                DialogResult result = XtraMessageBox.Show("Bạn muốn phê duyệt phiếu " + loginfo.Properties.ID, "Thông báo", MessageBoxButtons.YesNo);
                                if (result == DialogResult.Yes)
                                {
                                    loginfo.Properties.Status = true;
                                    loginfo.Save();
                                    Training_ScoreInfo scoreinfo = new Training_ScoreInfo(loginfo.Properties.Training_StudentKey.ToString(),
                                        int.Parse(loginfo.Properties.Training_Subject_ClassKey.ToString()),
                                        int.Parse(loginfo.Properties.Training_ScoreTypeKey.ToString()));
                                    if (scoreinfo.Properties.Training_Subject_ClassKey > 0)
                                    {
                                        try
                                        {
                                            dlg = new DevExpress.Utils.WaitDialogForm(MsgConfig.MSG_WAIT, MsgConfig.MSG_LOADING);
                                            // Cập nhật cột điểm vừa thay đổi
                                            scoreinfo.Properties.Score = loginfo.Properties.NewScore;
                                            scoreinfo.Save();

                                            // Lấy danh sách các loại điểm
                                            DataTable m_tblScoreType = new DataTable();
                                            m_tblScoreType = (m_data.m_DCReadOnly.Training_Scores.GetListScoreType(m_data.m_DCReadOnly.Training_SubjectClass_ScoreTypes, loginfo.Properties.Training_StudentKey.ToString(),
                                                int.Parse(loginfo.Properties.Training_Subject_ClassKey.ToString()))).ToDataTable();
                                            if (m_tblScoreType.Rows.Count != 0)
                                            {
                                                // Kiểm tra độ chênh lệch điểm cũ
                                                Double m_difference = 0;
                                                Training_Student_Subject_ClassInfo subclassInfo = new Training_Student_Subject_ClassInfo(loginfo.Properties.Training_StudentKey.ToString(),
                                                    int.Parse(loginfo.Properties.Training_Subject_ClassKey.ToString()));
                                                if (subclassInfo.Properties.Training_Subject_ClassKey > 0)
                                                    m_difference = Convert.ToDouble(subclassInfo.Properties.AverageScoreFinal) - Convert.ToDouble(subclassInfo.Properties.AverageScore);

                                                // Tính điểm vừa chỉnh sửa
                                                for (int i = 0; i < m_tblScoreType.Rows.Count; i++)
                                                {
                                                    if ((int.Parse(m_tblScoreType.Rows[i]["Training_ScoreTypeKey"].ToString()) == 3) && (GetValueLookUpEdit.ConvertValueToInt(m_tblScoreType.Rows[i]["Score"]) > 0))
                                                    {
                                                        // Xóa cột điểm lần 1 khi có cột điểm lần 2
                                                        for (int j = 0; j < m_tblScoreType.Rows.Count; j++)
                                                        {
                                                            if (int.Parse(m_tblScoreType.Rows[j]["Training_ScoreTypeKey"].ToString()) == 2)
                                                            {
                                                                m_tblScoreType.Rows.RemoveAt(j);
                                                                break;
                                                            }
                                                        }
                                                        break;
                                                    }
                                                }

                                                double m_score = 0;
                                                // Tính điểm
                                                for (int i = 0; i < m_tblScoreType.Rows.Count; i++)
                                                {
                                                    try { m_score = m_score + (Convert.ToDouble(m_tblScoreType.Rows[i]["Score"]) * int.Parse(m_tblScoreType.Rows[i]["DefaultPercent"].ToString())) / 100; }
                                                    catch { continue; }
                                                }
                                                // Làm tròn 1 số
                                                m_score = Math.Round(m_score, 2);

                                                double m_scoreFinal = m_score;
                                                // Kiểm tra điểm vừoa chỉnh sửa để cộng thêm nếu có điểm cộng
                                                if ((m_difference > 0) && (m_score + m_difference <= 10))
                                                    m_scoreFinal = m_score + m_difference;

                                                // Cập nhật điểm trung bình
                                                subclassInfo = new Training_Student_Subject_ClassInfo(loginfo.Properties.Training_StudentKey.ToString(),
                                                    int.Parse(loginfo.Properties.Training_Subject_ClassKey.ToString()));
                                                if (subclassInfo.Properties.Training_Subject_ClassKey > 0)
                                                {
                                                    subclassInfo.Properties.AverageScore = m_score;
                                                    subclassInfo.Properties.AverageScoreFinal = m_scoreFinal;
                                                    subclassInfo.Save();
                                                }

                                                // Cập nhật điểm trung bình tích lũy
                                                DataTable _dtListScore = new DataTable();
                                                _dtListScore = m_data.m_DCReadOnly.Training_Scores.GetSubjectScoreListofStudentByID(
                                                    m_data.m_DCReadOnly.Training_Subjects,
                                                    m_data.m_DCReadOnly.Training_TrainingProgram_Details,
                                                    m_data.m_DCReadOnly.Training_Student_Subject_Classes,
                                                    m_data.m_DCReadOnly.Training_Subject_Classes,
                                                    m_data.m_DCReadOnly.Training_Students,
                                                    m_data.m_DCReadOnly.Training_Student_Classes,
                                                    m_data.m_DCReadOnly.Catalogue_Terms,
                                                    m_data.m_DCReadOnly.Catalogue_SchoolYears,
                                                    scoreinfo.Properties.Training_StudentID, -1, -1, int.Parse(loginfo.Properties.Training_TrainingProgramKey.ToString())).ToDataTable();
                                                caculateAverageScoreAccumulate(_dtListScore, scoreinfo.Properties.Training_StudentID);

                                                dlg.Close();
                                                XtraMessageBox.Show("Đã cập nhật điểm", "Thông báo");
                                                GetListCensorship(m_iDepartmentKey);
                                            }
                                        }
                                        catch { XtraMessageBox.Show("Không thể cập nhật dữ liệu", "Thông báo"); }
                                    }
                                }
                            }
                            else
                            {
                                Log_ScoreEditInfo loginfo = new Log_ScoreEditInfo(m_iLogKey);
                                DialogResult result = XtraMessageBox.Show("Bạn không muốn phê duyệt phiếu " + loginfo.Properties.ID, "Thông báo", MessageBoxButtons.YesNo);
                                if (result == DialogResult.Yes)
                                {
                                    loginfo.Properties.Status = false;
                                    loginfo.Save();
                                    GetListCensorship(m_iDepartmentKey);
                                }
                            }
                        }
                    }
            }
        }

        private void SetStatus(ref Leader_CensorshipInfo cenInfo, int value)
        {
            if (value == 0)
                cenInfo.Properties.Status = null;
            else
                if (value == 1)
                    cenInfo.Properties.Status = true;
                else
                    cenInfo.Properties.Status = false;
        }

        private void grvView_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            //GridView View = sender as GridView;
            //if (!View.IsFilterRow(e.RowHandle))// && !View.IsGroupRow(e.RowHandle))
            //{
            //    bool status = Convert.ToBoolean(View.GetRowCellValue(e.RowHandle, View.Columns["Status"]));
            //    if (status == false)
            //        e.Appearance.BackColor = Color.Blue;
            //}
        }

        private void caculateAverageScoreAccumulate(DataTable dtInfo, string strStudentID)
        {
            DataTable _dtScore = dtInfo;
            string _strStudentId = strStudentID;
            _dtScore.Columns.Add("HPTC");
            double _dCreditAcc = 0;
            double _dScoreAcc = 0;
            double _dCredit = 0;
            double _dScore = 0;
            for (int i = 0; i < _dtScore.Rows.Count; i++)
            {
                _dtScore.Rows[i]["HPTC"] = Convert.ToDouble(_dtScore.Rows[i]["AverageScoreFinal"].ToString()) *
                                        Convert.ToDouble(_dtScore.Rows[i]["TotalCredits"].ToString());
                if (Convert.ToDouble(_dtScore.Rows[i]["AverageScoreFinal"].ToString()) >= DefaultConfig.MinScorePass_NoYearly)
                {
                    _dCreditAcc += Convert.ToDouble(_dtScore.Rows[i]["TotalCredits"].ToString());
                    _dScoreAcc += Convert.ToDouble(_dtScore.Rows[i]["HPTC"].ToString());
                }
                _dCredit += Convert.ToDouble(_dtScore.Rows[i]["TotalCredits"].ToString());
                _dScore += Convert.ToDouble(_dtScore.Rows[i]["HPTC"].ToString());
            }
            double averageScoreAcc = 0;
            if (_dScoreAcc != 0)
            {
                averageScoreAcc = Math.Round(_dScoreAcc / _dCreditAcc, 2, MidpointRounding.AwayFromZero);
            }
            double averageScore = 0;
            if (_dScore != 0)
            {
                averageScore = Math.Round(_dScore / _dCredit, 2, MidpointRounding.AwayFromZero);
            }
            Training_Student_ClassInfo infoStudentClass = new Training_Student_ClassInfo(_strStudentId);
            infoStudentClass.Properties.AverageScoreAccumulate = averageScoreAcc;
            infoStudentClass.Properties.AverageScoreNoAccumulate = averageScore;
            infoStudentClass.Properties.TotalCreditAccumulate = _dCreditAcc;
            infoStudentClass.Save();
        }
    }
}
